import { Component } from 'react';
import {Boxs, SearchBar, ScrollList, Button, Toast, Dialog} from 'saltui';
const { HBox, Box } = Boxs;
import { URLS,nodataIcon } from '../../../app/variables';
import DB from '../../../app/db';

const ApplyBoardroomItem  =  React.createClass({
  detailsClick: function(event) {
    let t = this;
    DB.SomeModuleAPI.deleteApplyBoardroom({id:t.props.id}).then((content) => {
      Toast.show({
        type: 'success',
        content: '取消成功'
      });
      t.props.refreshList();
    }).catch((error) => {
      if(typeof(error.errorMsg) == 'undefined'){
        Toast.show({
        type: 'error',
        content: '取消出错'
         });
      }else{
        Toast.show({
        type: 'error',
        content: error.errorMsg
         });
      }
    });
  },
  click:function(url){
    if(url){
      location.hash = url;
    }
  },
  render:function() {
    let now = new Date();
    var endTime = new Date(this.props.end_time+":00");
       return <div>         
        <HBox className="t-BCf t-P16 t-PR0 dd-bottom-border">
          <Box flex={1}>
            <div onClick={this.click.bind(this,"meeting/myMeetingDetails/"+this.props.id+"/myReceived")} className="t-omit t-FC3 t-FS16 t-LH1_3 t-MR8">
              {this.props.boardRoomName}
            </div>
            <HBox className="t-FCddfontblue-40 t-LH1_3 t-PT8 t-FS14" vAlign="center">
              <span className="t-omit">{this.props.start_time}~{this.props.end_time}</span>
            </HBox>
          </Box>
          <Box>
            <Button className={(this.props.boardroomStatus == 0) && (now.getTime() < endTime.getTime()) ? "t-R20 t-MR8" : "t-DN"} type="secondary" size="small" display="inline" onClick={this.detailsClick}>取消</Button>
            <Box className={this.props.boardroomStatus == 1 ? 't-FAC t-RL12 t-BCd t-FCf t-H22 t-LH22 t-FS12 t-W56' : 't-DN'}>已使用</Box>
          </Box>
        </HBox>           
      </div>
    }
 });

 export default class PageHome extends Component {
  
  constructor(props){
    super(props);
    Dialog.hide();
    //关闭加载页的遮罩效果
    $(".loading-containter").hide();
    //禁用ios弹性事件
    dd.ui.webViewBounce.disable();
    var winHeight = getScrollHigh(1, 0, 0);
    this.state = {
      search:'',
      noDataTip:'暂无数据',
      winHeight:winHeight,
    };
  }

  componentDidMount(){
    var page_title = "已预定会议室";
    $("title").text(page_title);
    var thirdMenuType = getLoginUserInfo().thirdMenuType;
    if(thirdMenuType == 'DING'){
      dd.biz.navigation.setTitle({
        title : page_title,
        onSuccess : function(result) {},
        onFail : function(err) {}
      });
    }else if(thirdMenuType =='MH'){
      !!emp&&emp.window.setTitle({
        title: {
            text: page_title,
            icon: '',
            actionmenu: {}
        },
      });
    }
    searchBarLimit(20);
  }

  beforeFetch(data,from){
    if (from.title == undefined) {
      data.condition = this.state.search;
    } else {
      data.condition = from.title;
    }
    return data;
  }

  fetchData(param){
    this.refs.meeting_applyBoardroom_ref.fetchData({title:''});
  }

  //搜索
  onSearch(keyword) {
    this.setState({
      search:keyword,
      noDataTip:'无搜索结果'
    });
    this.refs.meeting_applyBoardroom_ref.fetchData({title:keyword});
 }

 //推出搜索
 onExit(keyword){
  this.setState({
    search:'',
    noDataTip:'暂无数据'
  });
  this.refs.meeting_applyBoardroom_ref.fetchData({title:''});
}

  render(){
    let listHeight = {height: this.state.winHeight};
    return(
      <div>
        <SearchBar 
          hasHistory={false} 
          instantSearch={false} 
          onSearch={this.onSearch.bind(this)}  
          ref='applyBoardroom_search' 
          placeholder='请输入会议室名称' 
          onExit={this.onExit.bind(this)} />
        <div style={listHeight}>
          <ScrollList
            url={URLS.applyBoardroomList}
            dataType="json"
            pageSize={10}
            beforeFetch={this.beforeFetch.bind(this)}
            noDataTip={this.state.noDataTip}
            processData={(data) => data}
            ref = 'meeting_applyBoardroom_ref'
            noDataImage={nodataIcon}
            scrollLoad={true}
            currentPageKey="page">
            <ApplyBoardroomItem refreshList={this.fetchData.bind(this)}/>
          </ScrollList>
        </div>
      </div>
    )
  }
}